package site.shihuan.helpdesk.ticket.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import site.shihuan.helpdesk.common.model.JwtUser;
import site.shihuan.helpdesk.common.model.RestResult;
import site.shihuan.helpdesk.common.model.RestResultUtils;
import site.shihuan.helpdesk.security.context.UserContext;
import site.shihuan.helpdesk.ticket.manager.TicketLogManager;
import site.shihuan.helpdesk.ticket.manager.TicketManager;
import site.shihuan.helpdesk.ticket.model.po.TktTicketPO;

/**
 * 工单API
 *
 * @author 周世焕
 * @Date 2021/4/4 15:47
 */
@RestController
@RequestMapping("/v1/ticket")
public class TicketController {

    @Autowired
    private TicketManager ticketManager;

    @Autowired
    private TicketLogManager logManager;

    /**
     * 查询所有工单
     * @return
     */
    @GetMapping("all")
    public RestResult findAllTicket() {
        String tenantId = UserContext.getTenantId();
        return RestResultUtils.success(ticketManager.findAllTicket(tenantId));
    }

    /**
     * 查询组内工单
     * @return
     */
    @GetMapping("dept")
    public RestResult findDeptTicket() {
        JwtUser user = UserContext.getUser();
        return RestResultUtils.success(ticketManager.findDeptTicket(user.getTenantId(),user.getDeptId()));
    }

    /**
     * 查询我的工单
     * @return
     */
    @GetMapping("mine")
    public RestResult findMyTicket(@RequestParam String status) {
        JwtUser user = UserContext.getUser();
        return RestResultUtils.success(ticketManager.findUserTicket(user.getTenantId(),user.getUid(),status));
    }

    /**
     * 查询客户的工单
     * @return
     */
    @GetMapping("customer")
    public RestResult findCustomerTicket(@RequestParam String customerId, String status) {
        JwtUser user = UserContext.getUser();
        return RestResultUtils.success(ticketManager.findCustomerTicket(user.getTenantId(),customerId,status));
    }

    /**
     * 根据Id查询工单
     * @param ticketId
     * @return
     */
    @GetMapping
    public RestResult findById(@RequestParam String ticketId) {
        return RestResultUtils.success(ticketManager.findById(ticketId));
    }

    /**
     * 分配工单
     * @param ticketId
     * @return
     */
    @PostMapping("assign")
    public RestResult assign(@RequestParam String ticketId, @RequestParam String assignee, @RequestParam String assigneeName) {
        ticketManager.assignTicket(UserContext.getTenantId(),ticketId,assignee,assigneeName);
        return RestResultUtils.success();
    }

    /**
     * 分配工单（批量）
     * @param ticketId
     * @return
     */
    @PostMapping("assign/batch")
    public RestResult assign(@RequestParam String[] ticketId, @RequestParam String assignee, @RequestParam String assigneeName) {
        ticketManager.assignTicket(UserContext.getTenantId(),ticketId,assignee,assigneeName);
        return RestResultUtils.success();
    }

    /**
     * 删除工单（批量）
     * @param ticketId
     * @return
     */
    @DeleteMapping("batch")
    public RestResult delete(@RequestParam String[] ticketId) {
        ticketManager.deleteById(ticketId);
        return RestResultUtils.success();
    }

    /**
     * 删除工单
     * @param ticketId
     * @return
     */
    @DeleteMapping
    public RestResult delete(@RequestParam String ticketId) {
        ticketManager.deleteById(ticketId);
        return RestResultUtils.success();
    }

    /**
     * 查询工单历史
     * @param ticketId
     * @return
     */
    @GetMapping("history")
    public RestResult findHistoryById(@RequestParam String ticketId) {
        return RestResultUtils.success(logManager.findByTicketId(ticketId));
    }

    /**
     * 保存工单
     * @param ticketPO
     * @return
     */
    @PostMapping
    public RestResult saveTicket(@RequestBody TktTicketPO ticketPO) {
        ticketPO.setTenantId(UserContext.getTenantId());
        return RestResultUtils.success(ticketManager.saveTicket(ticketPO));
    }

    /**
     * 查询工单历史
     * @param ticketId
     * @return
     */
    @PostMapping("status")
    public RestResult changeTicketStatus(@RequestParam String ticketId,@RequestParam String old,
                                         @RequestParam String now) {
        ticketManager.changeTicketStatus(UserContext.getTenantId(),ticketId, old, now);
        return RestResultUtils.success();
    }
}
